package com.runda.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import tk.mybatis.mapper.entity.Example;

import com.bbjob.dto.Result;
import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.mapper.SysOrganizationStuMapper;
import com.bbjob.model.ExamInfo;
import com.bbjob.model.SysOrganization;
import com.github.pagehelper.PageInfo;
import com.runda.query.QueryExamInfo;
import com.runda.service.IExamInfoService;

@Api(tags = "examinfoApi", description = "考试管理")
@RestController
@RequestMapping(path = "examinfo", produces = "application/json;charset=utf-8")
public class ExamInfoController {

	@Resource
	private IExamInfoService examInfoService;

	@Resource
	private SysOrganizationMapper sysOrganizationMapper;

	@Resource
	private SysOrganizationStuMapper sysOrganizationStuMapper;

	@ApiOperation(value = "考试录入", notes = "考试录入")
	@ApiImplicitParam(name = "exam", value = "考试录入", required = true, dataType = "ExamInfo")
	@RequestMapping(value = "entry", method = RequestMethod.POST)
	public Result entryExamInfo(@RequestBody ExamInfo exam) {

		if (exam.getUniquecode() != null && !"".equals(exam.getUniquecode())) {
			Example example = new Example(SysOrganization.class);
			example.createCriteria().andEqualTo("uniqueCode", exam.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper
					.selectByExample(example);
			if (organs.size() > 0) {
				exam.setOrganId(organs.get(0).getId());
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			try {
				if (exam.geteEndtimeStr() != null && !"".equals(exam.geteEndtimeStr())) {
					exam.seteEndtime(sdf.parse(exam.geteEndtimeStr()));
				}
				
				if (exam.geteStarttimeStr() != null && !"".equals(exam.geteStarttimeStr())) {
					exam.seteStarttime(sdf.parse(exam.geteStarttimeStr()));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			exam.setCreateTime(new Date());
			exam.setUpdateTime(new Date());

			examInfoService.save(exam);
			return new Result(exam);
		} else {
			return new Result(8106, "学校唯一编码为空，无法查询学校信息");
		}
	}

	@ApiOperation(value="试卷-考生导入", notes="试卷-考生导入")
	@RequestMapping(value="import", method=RequestMethod.POST)
	public Map<String, Object> importExaminee(HttpServletRequest request,  @RequestParam(value="file", required=false) MultipartFile multipartFile) throws Exception {
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		
		try {
			returnMap = examInfoService.importExaminee(multipartFile, request, null);
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			
			returnMap.put("code", 8106);
			returnMap.put("message", "导入出现异常，请查看控制台信息");
			returnMap.put("data", null);
		}
		
		return returnMap;
	}
	
	
	@ApiOperation(value = "考试信息列表", notes = "考试信息列表")
	@ApiImplicitParam(name = "exam", value = "考试信息", required = true, dataType = "QueryExaminee")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public Result list(@RequestBody QueryExamInfo exam) {

		List<ExamInfo> data = examInfoService.selectByParams(exam);
		PageInfo<ExamInfo> pageInfo = new PageInfo<ExamInfo>(data);

		return new Result(pageInfo);
	}

	@ApiOperation(value = "考试信息", notes = "考试信息")
	@ApiImplicitParam(name = "id", value = "考试信息id", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "get/{id}", method = RequestMethod.GET)
	public Result get(@PathVariable("id") Integer id) {

		return new Result(examInfoService.selectByPrimaryKey(id));
	}

	@ApiOperation(value = "修改考试信息", notes = "修改考试信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "考试信息id", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "exam", value = "考试信息", required = true, dataType = "ExamInfo") })
	@RequestMapping(value = "edit/{id}", method = RequestMethod.POST)
	public Result edit(@PathVariable("id") Integer id,
			@RequestBody ExamInfo exam) {

		exam.setId(id);
		exam.setUpdateTime(new Date());
		this.examInfoService.updateNotNull(exam);

		return new Result();
	}

	@ApiOperation(value = "删除考试信息", notes = "删除考试信息")
	@ApiImplicitParam(name = "id", value = "考试信息id", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "del/{id}", method = RequestMethod.GET)
	public Result del(@PathVariable("id") Integer id) {

		examInfoService.deleteAll(id);

		return new Result();
	}
}
